﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers.SinhVienService;
using eTMS.Controllers;
using eTMS.Commons;
using eTMS.Forms;

namespace eTMS.UserControls
{
    public partial class SinhVienInfoInput : eTMS.Controls.UserControlBase
    {
        public SinhVienInfoInput()
        {
            InitializeComponent();
        }
        #region Variables
        private SinhVienController controller;
        public void SetController(SinhVienController _controller)
        {
            controller = _controller;
        }

        private bool _IsTwoCol = false;
        public bool IsTwoCol
        {
            get
            {
                return _IsTwoCol;
            }
            set
            {
                _IsTwoCol = value;
            }
        }
        #endregion
        #region Screen Functions
        //Load All lable text
        private void LoadLanguage()
        {
            this.Text = Constants.APP_ADDNEW + " " + SinhVienStringMapping.SinhVien;
            labelMaSinhVien.Text = SinhVienStringMapping.MaSinhVien;
            labelTenSinhVien.Text = SinhVienStringMapping.TenSinhVien;
            labelDiaChi.Text = SinhVienStringMapping.DiaChi;
            labelEmail.Text = SinhVienStringMapping.Email;
            labelSoCMND.Text = SinhVienStringMapping.SoCMND;
            labelDienThoai.Text = SinhVienStringMapping.DienThoai;
            labelGhiChu.Text = SinhVienStringMapping.GhiChu;
            labelGioiTinh.Text = SinhVienStringMapping.GioiTinh;
            labelNgaySinh.Text = SinhVienStringMapping.NgaySinh;
        }

        //Lock-Unlock control in screen
        private void LockScreen(bool IsLocked)
        {
            txtMaSinhVien.ReadOnly = IsLocked;
            txtTenSinhVien.ReadOnly = IsLocked;
            txtDiaChi.ReadOnly = IsLocked;
            txtEmail.ReadOnly = IsLocked;
            txtSoCMND.ReadOnly = IsLocked;
            txtDienThoai.ReadOnly = IsLocked;
            txtGhiChu.ReadOnly = IsLocked;
            txtGioiTinh.ReadOnly = IsLocked;
            txtNgaySinh.ReadOnly = IsLocked;
        }
        public void UpdateToModel()
        {
            if (controller.SinhVienObject == null)
                controller.ResetObject();
            CopyFromSreen(controller.SinhVienObject);
        }
        //Bind data from object to control in screen
        private void CopyToSreen(SinhVienEntity o)
        {
            txtGioiTinh.BindData();
            if (!o.IsNullMaSinhVien)
            {
                txtMaSinhVien.Text = Convert.ToString(o.MaSinhVien);
            }
            if (!o.IsNullTenSinhVien)
            {
                txtTenSinhVien.Text = Convert.ToString(o.TenSinhVien);
            }
            if (!o.IsNullDiaChi)
            {
                txtDiaChi.Text = Convert.ToString(o.DiaChi);
            }
            if (!o.IsNullEmail)
            {
                txtEmail.Text = Convert.ToString(o.Email);
            }
            if (!o.IsNullSoCMND)
            {
                txtSoCMND.Text = Convert.ToString(o.SoCMND);
            }
            if (!o.IsNullDienThoai)
            {
                txtDienThoai.Text = Convert.ToString(o.DienThoai);
            }
            if (!o.IsNullGhiChu)
            {
                txtGhiChu.Text = Convert.ToString(o.GhiChu);
            }
            if (!o.IsNullNgaySinh)
            {
                txtNgaySinh.Text = Convert.ToString(o.NgaySinh);
            }
            if (!o.IsNullGioiTinh)
            {
                if (!(txtGioiTinh.IsAddAllItem || txtGioiTinh.IsAddChooseItem))
                {
                    txtGioiTinh.SelectedIndex = o.GioiTinh;
                }
                else
                {
                    txtGioiTinh.SelectedIndex = o.GioiTinh + 1;
                }
            }
        }
        //Copy data from control in screen to object
        private void CopyFromSreen(SinhVienEntity o)
        {
            o.MaSinhVien = Convert.ToString(txtMaSinhVien.Text);
            o.TenSinhVien = Convert.ToString(txtTenSinhVien.Text);
            o.DiaChi = Convert.ToString(txtDiaChi.Text);
            o.Email = Convert.ToString(txtEmail.Text);
            o.SoCMND = Convert.ToString(txtSoCMND.Text);
            o.DienThoai = Convert.ToString(txtDienThoai.Text);
            o.GhiChu = txtGhiChu.Text;
            if (txtGioiTinh.IsAddChooseItem)
            {

                o.GioiTinh = controller.ConvertByte(txtGioiTinh.SelectedValue);
            }
            else
            {
                o.GioiTinh = controller.ConvertByte(txtGioiTinh.SelectedValue);
            }
            o.NgaySinh = txtNgaySinh.Text;
        }
        public void UpdateToScreen()
        {
            if (controller.SinhVienObject == null)
                controller.ResetObject();
            CopyToSreen(controller.SinhVienObject);
        }
        //Validate all data from control in screen before Save data
        protected bool ValidateScreen()
        {
            bool result = true;
            if (txtMaSinhVien.Text.Trim() == "")
            {
                errorProviderData.SetError(txtMaSinhVien, Constants.MSS_ERROR_TEXT);
                result = false;
            }
            else
            {
                errorProviderData.SetError(txtMaSinhVien, "");
            }
            if (txtTenSinhVien.Text.Trim() == "")
            {
                errorProviderData.SetError(txtTenSinhVien, Constants.MSS_ERROR_TEXT);
                result = false;
            }
            else
            {
                errorProviderData.SetError(txtTenSinhVien, "");
            }
            if (!result)
            {
                ShowInfo(Constants.MSS_REQUIRE_FIELD);
            }
            if (result)
            {
                //Check if exists MaSinhVien				
                bool existsMaSinhVien = controller.CheckExistsMaSinhVien(txtMaSinhVien.Text);
                if (existsMaSinhVien)
                {
                    result = false;
                    errorProviderData.SetError(txtMaSinhVien, Constants.MSS_ERROR_DATA_UNIQUE);
                    ShowInfo(Constants.MSS_ERROR_EXISTS_DATA);
                }
                else
                {
                    errorProviderData.SetError(txtMaSinhVien, "");
                }
            }
            return result;
        }
        #endregion
        #region Business
        //AddNew data to database
        public bool Save()
        {
            if (controller.SinhVienObject == null)
            {
                controller.ResetObject();
            }
            CopyFromSreen(controller.SinhVienObject);
            bool rval = true;
            if (controller.SinhVienObject.SinhVien_ID == 0)
            {
                rval = controller.AddNew();
            }
            else
            {
                rval = controller.Update();
            }
            return rval;
        }
        #endregion
        #region Windows Control EventHandlers
        private void SinhVienInfoInput_Load(object sender, EventArgs e)
        {
            LoadLanguage();
            if (_IsTwoCol)
            {
                txtDiaChi.Anchor = txtNgaySinh.Anchor;
                txtEmail.Anchor = txtNgaySinh.Anchor;
                txtGhiChu.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
                labelDiaChi.Location = new Point(labelGioiTinh.Location.X, labelGioiTinh.Location.Y);
                txtDiaChi.Location = new Point(txtGioiTinh.Location.X, txtGioiTinh.Location.Y); ;
                txtDiaChi.Width = txtTenSinhVien.Right - txtDiaChi.Left;
                int delta = labelGioiTinh.Top - labelMaSinhVien.Top;
                labelGioiTinh.Top += delta;
                txtGioiTinh.Top += delta;
                labelNgaySinh.Top += delta;
                txtNgaySinh.Top += delta;


                labelSoCMND.Top += delta;
                txtSoCMND.Top += delta;
                labelDienThoai.Top += delta;
                txtDienThoai.Top += delta;

                labelEmail.Left = labelSoCMND.Left;
                labelEmail.Top = labelSoCMND.Top + delta;
                txtEmail.Left = txtSoCMND.Left;
                txtEmail.Top = txtSoCMND.Top + delta;
                txtEmail.Width = txtDiaChi.Width;
                labelGhiChu.Top = txtEmail.Top + delta;
                txtGhiChu.Top = txtEmail.Top + delta;
                txtGhiChu.Width = txtDiaChi.Width;
                int h = this.panelDetail.Height - txtGhiChu.Top - 3;
                if (h > 0)
                    txtGhiChu.Height = h;
            }
        }

        #endregion

        public string GetFilterSinhVien()
        {
            List<string> lsFilter=new List<string>();
            if (controller.SinhVienObject.TenSinhVien != null)
            {
                if (controller.SinhVienObject.TenSinhVien.Length > 0)
                {
                    lsFilter.Add("TenSinhVien Like '%"+controller.SinhVienObject.TenSinhVien+"%'");
                }
            }
            if (controller.SinhVienObject.SoCMND != null)
            {
                if (controller.SinhVienObject.SoCMND.Length > 0)
                {
                    lsFilter.Add("SoCMND='"+controller.SinhVienObject.SoCMND+"'");
                }
            }
            if (controller.SinhVienObject.NgaySinh != null)
            {
                if (controller.SinhVienObject.NgaySinh.Length > 0)
                {
                    lsFilter.Add("NgaySinh='"+controller.SinhVienObject.NgaySinh+"'");
                }
            }
            if (controller.SinhVienObject.MaSinhVien != null)
            {
                if (controller.SinhVienObject.MaSinhVien.Length > 0)
                {
                    lsFilter.Add("MaSinhVien='"+controller.SinhVienObject.MaSinhVien+"'");
                }
            }
            if (controller.SinhVienObject.GioiTinh <= 1)
            {
                lsFilter.Add("GioiTinh='"+controller.SinhVienObject.GioiTinh+"'");
            }
            if (controller.SinhVienObject.GhiChu != null)
            {
                if (controller.SinhVienObject.GhiChu.Length > 0)
                {
                    lsFilter.Add("GhiChu Like '%"+ controller.SinhVienObject.GhiChu+"%'");
                }
            }
            return string.Join(" AND ", lsFilter.ToArray());
        }
        
    }
}
